package com.hk.yajin.function.messages;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.hk.developmentlib.utills.DateUtils;
import com.hk.developmentlib.utills.StringHelper;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.model.MessageType;
import com.hk.yajin.model.MessageTypeChildBean;

import java.util.List;

import static com.hk.yajin.R.id.tv_buy_all;

/**
 * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 */
public class BuyMessageMultQuickAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public final static int TYPE_PARENT = 0;
    public final static int TYPE_CHILD = 1;
    private Context context;
    private OnlickItemClick onlickItemClickCallBack;

    public BuyMessageMultQuickAdapter(List<MultiItemEntity> datas, Context ct, OnlickItemClick activity) {
        super(datas);
        context = ct;
        addItemType(0, R.layout.list_item_buy_message_parent);
        addItemType(1, R.layout.list_item_buy_message_child);
        onlickItemClickCallBack = activity;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity messageType) {
        switch (holder.getItemViewType()) {
            case TYPE_PARENT:
                final MessageType parent = (MessageType) messageType;
                handleParentClick(parent, holder);
                holder.setText(R.id.tv_parent_name, parent.getClass_name());

                holder.setText(R.id.tv_parent_sum, (parent.getSummary() + "文本简介"));
                if (parent.getSubscribe() == 1) {//已订购
                    holder.setText(R.id.tv_buy_all, "");
                    holder.setVisible(R.id.tv_buy_all,false);
                    holder.setText(R.id.tv_parent_gold,"已订阅至:"+ DateUtils.getMessageYMD(((MessageType) messageType).getEnd_time()));
                    holder.setTextColor(R.id.tv_parent_gold, UiUtil.getColor(R.color.oriag2));
                } else {                         //未订购
                    holder.setText(R.id.tv_buy_all, UiUtil.getString(R.string.baoyuedinggou));
                    holder.setVisible(R.id.tv_buy_all,true);
                    holder.setText(R.id.tv_parent_gold, parent.getDuration_gold() + context.getString(R.string.gold));
                    holder.setTextColor(R.id.tv_parent_gold, UiUtil.getColor(R.color.oriag2));
                }


                holder.getView(tv_buy_all).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onlickItemClickCallBack == null||parent.getSubscribe() == 1) return;
                        onlickItemClickCallBack.onClickParent(parent,holder.getAdapterPosition());
                    }
                });
                final ImageView arrow = holder.getView(R.id.iv_arrow);
                if (parent.isExpanded()) {
                    arrow.setRotation(90);
                    holder.setVisible(R.id.tv_parent_sum, true);
                } else {
                    arrow.setRotation(0);
                    holder.setVisible(R.id.tv_parent_sum, false);
                }
                break;
            case TYPE_CHILD:
                final MessageTypeChildBean childdata = (MessageTypeChildBean) messageType;
                holder.setText(R.id.tv_child_name, ((MessageTypeChildBean) messageType).getClass_name());
                holder.setText(R.id.tv_child_gold, ((MessageTypeChildBean) messageType).getOnce_gold() + "金币");
                if (childdata.getSubscribe() == 1) {
                    holder.setText(R.id.tv_my_video, UiUtil.getString(R.string.tuiding));
                    holder.setBackgroundRes(R.id.tv_my_video, R.drawable.radius10_gray);
                } else {
                    holder.setText(R.id.tv_my_video, UiUtil.getString(R.string.dingyue));
                    holder.setBackgroundRes(R.id.tv_my_video, R.drawable.radius10_blue);
                }
                holder.getView(R.id.tv_my_video).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onlickItemClickCallBack == null) return;
                        onlickItemClickCallBack.onClickChilde(childdata,getParentPosition(messageType),holder.getLayoutPosition());
                    }
                });

                MessageType parentdata = (MessageType) getData().get(getParentPosition(messageType));
                if (parentdata.getChild().size() + getParentPosition(messageType) == holder.getLayoutPosition()) {
                    holder.getView(R.id.line).getLayoutParams().height = UiUtil.dip2px(10);
                } else {
                    holder.getView(R.id.line).getLayoutParams().height = UiUtil.dip2px(0);
                }

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        LogUtil.d("holder.getLayoutPosition():"+holder.getLayoutPosition());
//                        LogUtil.d("holder.getAdapterPosition():"+holder.getAdapterPosition());
//                        LogUtil.d("getParentPosition(messageType):"+getParentPosition(messageType));
//                        LogUtil.d("holder.getOldPosition():"+holder.getOldPosition());

                    }
                });

        }
    }


    private void handleParentClick(final MessageType parent, final BaseViewHolder holder) {

        final int pos = holder.getAdapterPosition();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (parent.isExpanded()) {
                    collapse(pos, false);
                } else {
                    expand(pos, false);
                }
            }
        });
    }


    public interface OnlickItemClick {
        void onClickParent(MessageType itemParent,int position);

        void onClickChilde(MessageTypeChildBean itemChild,int parentPositon,int childPosition);
    }

}
